var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var hasclick = false;
Page({
  data: {
    orderList: [],
    ordertype: 1,
    page: 1,
    limit: 10
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.ordertype) {
      this.setData({
        ordertype: options.ordertype
      });
    }
    this.setData({
      orderList: [],
      page: 1,
      limit: 10
    });
    if (hasclick) {
      return
    }
    hasclick = true;
    this.getOrderList();
  },

  //获取订单数据
  getOrderList() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    util.request(api.MyOrderRefundList, {
      ordertype: that.data.ordertype,
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.length > 0) {
          that.setData({
            orderList: that.data.orderList.concat(res.data),
            page: that.data.page + 1
          });
        }
        hasclick = false;
        wx.hideLoading();
      }
    });
    wx.hideLoading();
  },

  //下拉触底事件
  onReachBottom() {
    this.getOrderList();
  },

  //切换页签事件
  switchTab: function (event) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    if (hasclick) {
      return
    }
    hasclick = true;
    let ordertype = event.currentTarget.dataset.ordertype;
    that.setData({
      orderList: [],
      ordertype: ordertype,
      page: 1,
      limit: 10
    });
    that.getOrderList();
  },


  // “退款”点击效果
  tuikuan: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.index;
    wx.showModal({
      title: '',
      content: '确定要发起“退款”申请？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderRefund, {
            orderId: orderId
          }).then(function (res) {
            if (res.result == 1) {
              wx.showToast({
                title: '退款申请已发起！'
              });
              let _orderList = that.data.orderList;
              for (let i = 0; i < _orderList.length; i++) {
                if (_orderList[i].id == orderId) {
                  _orderList.splice(i, 1)
                }
              }
              that.setData({
                orderList: _orderList
              });
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },

  // “退换货”点击效果
  tuihuanhuo: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.index;
    wx.showModal({
      title: '',
      content: '确定要发起“退换货”申请？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderTuihuanhuo, {
            orderId: orderId
          }).then(function (res) {
            if (res.result == 1) {
              wx.showToast({
                title: '退换货申请已发起！'
              });
              let _orderList = that.data.orderList;
              for (let i = 0; i < _orderList.length; i++) {
                if (_orderList[i].id == orderId) {
                  _orderList.splice(i, 1)
                }
              }
              that.setData({
                orderList: _orderList
              });
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },

  // “退换货”点击效果
  xiangqing: function (e) {
    let that = this;
    let refundstatus = e.currentTarget.dataset.refundstatus;
    if (refundstatus == 1) {
      refundstatus = '退款申请中';
    }
    if (refundstatus == 2) {
      refundstatus = '待转账';
    }
    if (refundstatus == 3) {
      refundstatus = '退款结束';
    }
    if (refundstatus == 4) {
      refundstatus = '退换货申请中';
    }
    if (refundstatus == 5) {
      refundstatus = '卖家已收货';
    }
    if (refundstatus == 6) {
      refundstatus = '卖家已拒绝';
    }
    let maijiaremark = e.currentTarget.dataset.maijiaremark;
    if (maijiaremark == null) {
      maijiaremark = '处理中,请耐心等候!';
    }
    wx.showModal({
      title: '退换状态：' + refundstatus,
      content: '卖家回复：' + maijiaremark
    });
  },


  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})